package com.aaa.member.service.impl;

import com.aaa.entity.Coupon;
import com.aaa.entity.Shop;
import com.aaa.member.dao.CouponDao;
import com.aaa.member.service.CouponService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author JYH
 * @version 1.0
 * @date 2023/3/2 10:54
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponDao, Coupon> implements CouponService {

    @Autowired
    private CouponDao couponDao;
    @Override
    public Result selectAll(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage,pageSize);
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.like(name!=null,"name",name);
        List<Coupon> coupons = couponDao.selectList(wrapper);
        PageInfo<Coupon> pageInfo = new PageInfo<>(coupons);
        return new Result(2000,"成功",pageInfo);
    }

    @Override
    public Result add(Coupon coupon) {
        //获取当前日期
        Date date = new Date();

        //设置当前日期加一个月
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH,1);
        //
        Date endTime = rightNow.getTime();

        coupon.setStartTime(date);
        coupon.setEnableTime(date);
        coupon.setEndTime(endTime);
        int insert = couponDao.insert(coupon);
        return new Result(2000,"成功",insert);

    }

    @Override
    public Result selectShops() {
        List<Shop> shops = couponDao.selectShops();
        return new Result(2000,"成功",shops);
    }

    @Override
    public Result delete(long id) {
        int i = couponDao.deleteById(id);
        return new Result(2000,"成功",i);
    }
}
